<template>
  <div>

    <el-card style="margin-top: 2vh;width: 150vh;text-align: left;height: 10vh">
      <el-select v-model="page.statu" placeholder="请选择"
                 @change="load">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <template>
        <el-radio v-model="radio" label="1" @change="timeChange()">昨天</el-radio>
        <el-radio v-model="radio" label="2" @change="timeChange()">今天</el-radio>
        <el-radio v-model="radio" label="3" @change="timeChange()">本周</el-radio>
        <el-radio v-model="radio" label="4" @change="timeChange()">上月</el-radio>
        <el-radio v-model="radio" label="5" @change="timeChange()">本季</el-radio>
        <el-radio v-model="radio" label="6" @change="timeChange()">上季</el-radio>
        <el-radio v-model="radio" label="7" @change="timeChange()">今年</el-radio>
      </template>
      <el-button icon="el-icon-circle-plus-outline" @click="$router.push('/warehouse/Form')">填写采购单</el-button>
    </el-card>


    <el-table :data="tableData.slice((page.pageNum-1)*page.pageSize,page.pageNum*page.pageSize)" border stripe :header-cell-style="{background:'#ccc'}" :default-expand-all="false"
              row-key="id" @selection-change="handleSelectionChange" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="id" label="序号"></el-table-column>
      <el-table-column prop="orederNumber" label="单据编号"></el-table-column>
      <el-table-column prop="putNumber" label="商品数量"></el-table-column>
      <el-table-column prop="requestUser" label="制单人"></el-table-column>
      <el-table-column prop="createTime" label="制单日期" width="180"></el-table-column>
      <el-table-column prop="statu" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.statu==1">待提交</el-tag>
          <el-tag v-if="scope.row.statu==2">已提交</el-tag>
          <el-tag v-if="scope.row.statu==3">审核中</el-tag>
          <el-tag v-if="scope.row.statu==4">已审核</el-tag>
          <el-tag v-if="scope.row.statu==5">待发货</el-tag>
          <el-tag v-if="scope.row.statu==6">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="workUser" label="审核人"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>

      <el-table-column label="操作" align="center" fixed="right" width="90px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.statu==5" @click="handleAdd(scope.row)">发货<i class="el-icon-remove-outline"></i></el-tag>
        </template>
      </el-table-column>
    </el-table>

<!--        <el-pagination-->
<!--          :current-page="page.pageNum"-->
<!--          :page-sizes="[2, 3, 4, 5]"-->
<!--          :page-size="1"-->
<!--          layout="total, sizes, prev, pager, next, jumper"-->
<!--          :total="tableData.length"-->
<!--          @size-change="handleSizeChange"-->
<!--          @current-change="handleCurrentChange"-->
<!--        />-->

    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[2, 4, 8, 10]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length">
      </el-pagination>
    </div>

    <!--          添加/修改弹窗-->
    <el-dialog title="下单信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="80px" size="small">
        <el-form-item label="订单编号">
          <el-input v-model="form.orderNum" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="快递单号">
          <el-input v-model="form.expressNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="业务员">
          <el-input v-model="form.updateUser" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.updateInfo" autocomplete="off"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeStatus">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {putGoodsList,updateStatu} from "../../api/warehouse";

export default {
  name: "OrderList",
  data(){
    return {
      radio:"",
      options: [
        {value: '1',label: '待提交'},
        {value: '2',label: '已提交'},
        {value: '3',label: '审核中'},
        {value: '4',label: '已审核'},
        {value: '5',label: '待发货'},
        {value: '6',label: '已完成'}
      ],
      map:{
      },
      tableData: [],
      users: [],
      orderNum:"",
      multipleSelection: [],
      page:{
        pageNum: 1,
        pageSize: 4,
      },
      total: 0,
      form: {},
      dialogFormVisible: false,
      dialogImageUrl: '',
      dialogVisible: false,
      myHeaders:{'Access-Control-Allow-Origin':'*'},
      startTime:"",
      endTime:"",
    }
  },
  created() {
    this.load()
  },
  methods:{
    timeChange() {
      var date = new Date();
      if (this.radio == 1) {//昨天
        this.startTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate() - 1);
        this.endTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate());
      } else if (this.radio == 2) {//今天
        this.startTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate());
        this.endTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate() + 1);
      } else if (this.radio == 3) {//本周
        this.$message.info("本周以周日开始计算")
        let day=date.getDay()
        this.startTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate() - day);
        this.endTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate() + 7 - day);
      } else if (this.radio == 4) {//上月
        this.startTime = date.getFullYear() + "-" + (date.getMonth() ) + "-" + 1 ;
        this.endTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + 1;
      } else if (this.radio == 5) {//本季
        let month = date.getMonth()+1;
        if (month/3<=1){
          this.startTime = date.getFullYear() + "-" + 1 + "-" + 1;
          this.endTime = date.getFullYear() + "-" + 4 + "-" + 1;
        }else if (month/3<=2){
          this.startTime = date.getFullYear() + "-" + 4 + "-" + 1;
          this.endTime = date.getFullYear() + "-" + 7 + "-" + 1;
        }else if (month/3<=3){
          this.startTime = date.getFullYear() + "-" + 7 + "-" + 1;
          this.endTime = date.getFullYear() + "-" + 10 + "-" + 1;
        }else if (month/3<=4){
          this.startTime = date.getFullYear() + "-" + 10 + "-" + 1;
          this.endTime = (date.getFullYear()+1) + "-" + 1 + "-" + 1;
        }else {
          alert("出现错误请重写选择")
        }
      } else if (this.radio == 6) {//上季
        let month = date.getMonth()+1;
        if (month/3<=2){
          this.startTime = date.getFullYear() + "-" + 1 + "-" + 1;
          this.endTime = date.getFullYear() + "-" + 4 + "-" + 1;
        }else if (month/3<=3){
          this.startTime = date.getFullYear() + "-" + 4 + "-" + 1;
          this.endTime = date.getFullYear() + "-" + 7 + "-" + 1;
        }else if (month/3<=4){
          this.startTime = date.getFullYear() + "-" + 7 + "-" + 1;
          this.endTime = date.getFullYear() + "-" + 10 + "-" + 1;
        }else if (month/3<=1){
          this.startTime = date.getFullYear() + "-" + 10 + "-" + 1;
          this.endTime = (date.getFullYear()+1) + "-" + 1 + "-" + 1;
        }else {
          alert("出现错误请重写选择")
        }
      } else if (this.radio == 7) {//今年
        this.startTime = date.getFullYear() + "-" + 1 + "-" + 1;
        this.endTime = (date.getFullYear()+1) + "-" + 1 + "-" + 1;
      } else {
        alert("出现错误请重写选择")

      }
      this.$message.success(this.startTime + "至" + this.endTime)
      this.load()
    },
    load() {
      this.page.endTime=this.endTime
      this.page.startTime=this.startTime
      putGoodsList(this.page).then(res=>{
        if(res.code==200){
          this.tableData=res.data
          this.total = res.data.total
        }else{
          this.$message({
            type:'error',
            message:'请求角色列表失败'
          })
        }
      })

    },
    changeStatus(row) {
      console.log(this.form)
      // debugger
      updateStatu(this.form).then(res=>{
        this.$message.success("发货成功")
        this.dialogFormVisible = false
      })

      this.load()
    },
    del(id) {
      this.request.delete("/file/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/file/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.page.brandName = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.page.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.page.pageNum = pageNum
      this.load()
    },
    handleAdd(row){

      let user =JSON.parse(localStorage.getItem("userdata"))
      this.form = row
      this.form.orderNum=row.orederNumber
      this.form.type="采购单"
      this.form.updateUser=user.managerName

      this.dialogFormVisible = true

    },
    save(){
      addBrand(this.form).then(res=>{
        this.$message.success("添加成功")
      })
      console.log(this.form)
    },
    handleAvatarSuccess(res) {
      this.form.logo = res
      this.$message.success("上传成功")
    },

    uploadImg(param){
      const formData = new FormData()
      formData.append('file', param.file)
      // alert(formData)
      this.$axios.post("/management-mall/leyou.mall/mall-goods/test/oss",formData,{
        'Content-type' : 'multipart/form-data'
      }).then(response => {
        this.form.logo="https://"+response.data
      })
    },

    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  }
}
</script>

<style scoped>
.el-card{
  height: 5vh;
  margin-right:  5vh;
  font-size: 5px;
  text-align: center;
  line-height: 5px;
  color: #E6A23C;
  font-weight: bold;
}
</style>
